package cn.itcast.order.web;

import cn.itcast.feign.client.UserClient;


import cn.itcast.feign.pojo.User;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserClient userClient;


    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {

        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);

        //调用Feign发送其他服务模块发送http请求
        User user = userClient.findById(order.getUserId());

        //添加user
        order.setUser(user);
        //返回订单信息
        return order;
    }


//    @GetMapping("{orderId}")
//    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
//
//        // 根据id查询订单并返回
//        Order order = orderService.queryOrderById(orderId);
//
//        // 定义请求地址
//        String url = "http://userserver/user/"+order.getUserId();
//
//        //通过RestTemplate向user-service发起请求获取user
//        User user = restTemplate.getForObject(url, User.class);
//
//        //添加user
//        order.setUser(user);
//        //返回订单信息
//        return order;
//    }
}
